package com.shuwen.gcdj.controller.common;

import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.common.request.AddCommonFieldReq;
import com.shuwen.gcdj.bean.common.request.DeleteCommonFieldReq;
import com.shuwen.gcdj.bean.common.request.SearchCommonFieldListReq;
import com.shuwen.gcdj.bean.common.request.SearchPagingCommonFieldListReq;
import com.shuwen.gcdj.bean.common.request.UpdateCommonFieldReq;
import com.shuwen.gcdj.bean.common.response.IntegerStringKvItemResp;
import com.shuwen.gcdj.bean.common.response.SearchCommonFieldListItemResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.service.account.AccountService;
import com.shuwen.gcdj.service.common.CommonFieldService;
import com.shuwen.gcdj.service.common.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户中心 - 通用接口(可用)")
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController extends AuthController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private CommonFieldService commonFieldService;

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "信息范围授权枚举(可用)")
    @GetMapping(value = "/getInfoLevels")
    @ResponseBody
    public CommonResult<List<IntegerStringKvItemResp>> getInfoLevels() {
        return CommonResult.success(commonService.getInfoLevels());
    }

    @ApiOperation(value = "数据范围授权枚举(可用)")
    @GetMapping(value = "/getDataLevels")
    @ResponseBody
    public CommonResult<List<IntegerStringKvItemResp>> getDataLevels() {
        return CommonResult.success(commonService.getDataLevels());
    }

    @ApiOperation(value = "字段管理 - 添加(可用)")
    @PostMapping(value = "/addCommonField")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_COMMON_ADDCOMMONFIELD")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> addCommonField(HttpServletRequest request, @Valid @RequestBody AddCommonFieldReq req) {
        InitUserDto initUserDto = initUser(request);
        return commonFieldService.add(req, initUserDto);
    }

    @ApiOperation(value = "字段管理 - 编辑(可用)")
    @PostMapping(value = "/updateCommonField")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_COMMON_UPDATECOMMONFIELD")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> updateCommonField(HttpServletRequest request, @Valid @RequestBody UpdateCommonFieldReq req) {
        InitUserDto initUserDto = initUser(request);
        return commonFieldService.updateField(req, initUserDto);
    }

    @ApiOperation(value = "字段管理 - 删除(可用)")
    @PostMapping(value = "/deleteCommonField")
    @ResponseBody
    @JWTAuth(apiCode = "USER_API_COMMON_DELETECOMMONFIELD")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> deleteCommonField(HttpServletRequest request, @Valid @RequestBody DeleteCommonFieldReq req) {
        InitUserDto initUserDto = initUser(request);
        return commonFieldService.deleteField(req, initUserDto);
    }


    @ApiOperation(value = "字段管理 - 列表(分页)(可用)")
    @PostMapping(value = "/getPagingCommonFieldList")
    @ResponseBody
    public CommonResult<CommonPage<SearchCommonFieldListItemResp>> getCommonFieldList(HttpServletRequest request, @Valid @RequestBody SearchPagingCommonFieldListReq req) {
        return commonFieldService.getPagingCommonFieldList(req);
    }

    @ApiOperation(value = "字段管理 - 列表(不分页)(可用)")
    @PostMapping(value = "/getCommonFieldList")
    @ResponseBody
    public CommonResult<List<SearchCommonFieldListItemResp>> getCommonFieldList(HttpServletRequest request, @Valid @RequestBody SearchCommonFieldListReq req) {
        return commonFieldService.getCommonFieldList(req);
    }

    @ApiOperation(value = "加密 - 生成加密密码")
    @GetMapping(value = "/crypt/createSecretPwd/{originPwd}")
    @ResponseBody
    public CommonResult<String> singOriginPwd(@PathVariable String originPwd) {
        return CommonResult.success(accountService.singOriginPwd(originPwd));
    }
}
